package com.agg.core.utils;

import java.lang.reflect.Array;
import java.util.*;

public class CollectionsUtil {
    /**
     * 判断集合是否为空或者长度是否为0
     *
     * @param object
     * @return
     */
    public static boolean sizeIsEmpty(Object object) {
        if (object == null) {
            return true;
        }
        if ((object instanceof Collection)) {
            return ((Collection) object).isEmpty();
        }
        if ((object instanceof Map)) {
            return ((Map) object).isEmpty();
        }
        if ((object instanceof Object[])) {
            return ((Object[]) object).length == 0;
        }
        if ((object instanceof Iterator)) {
            return !((Iterator) object).hasNext();
        }
        if ((object instanceof Enumeration)) {
            return !((Enumeration) object).hasMoreElements();
        }
        try {
            return Array.getLength(object) == 0;
        } catch (IllegalArgumentException ex) {
            throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
        }
    }

    /**
     * 比较两个 集合是否相等
     * @param a
     * @param b
     * @param <T>
     * @return
     */
    public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) {
        if(a.size() != b.size()){
            return false;
        }
        //重新创建临时集合，避免排序后会影响到传入的参数
        List<T> temp1=new ArrayList<>();
        List<T> temp2=new ArrayList<>();
        a.forEach(e->temp1.add(e));
        b.forEach(e->temp2.add(e));
        Collections.sort(temp1);
        Collections.sort(temp2);
        for(int i=0;i<temp1.size();i++){
            if(!temp1.get(i).equals(temp2.get(i))){
                return false;
            }
        }
        return true;
    }
}
